package com.wdc.回溯;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;

@SuppressWarnings("all")
public class E09Leetcode90 {
    private List<List<Integer>> result = new ArrayList<>();
    private LinkedList<Integer> path = new LinkedList<>();
    private boolean[] userd;
    public List<List<Integer>> subsetsWithDup(int[] nums) {
        Arrays.sort(nums);
        userd = new boolean[nums.length];
        backtracking(nums,0);
        return result;
    }

    void backtracking(int[] nums , int startIndex){
        result.add(new ArrayList<>(path));
        //结束条件
        if (startIndex >= nums.length){
            return;
        }
        //循环宽度
        for (int i = startIndex; i < nums.length; i++) {
            //去重
            if (i > 0 && nums[i] == nums[i -1] && !userd[i - 1]){
                continue;
            }
            path.add(nums[i]);
            userd[i] = true;
            backtracking(nums,i + 1);
            path.removeLast();
            userd[i] = false;
        }

    }
}
